package cn.hnu.qq.client.panel;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.apache.log4j.Logger;

import cn.hnu.qq.client.controller.CrowdController;
import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.entity.Crowd;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.CrowdChartMessage;
import cn.hnu.qq.entity.message.Message;
import cn.hnu.qq.global.MessageType;

public class CrowdChartPanel extends JPanel implements ActionListener,MessageListener{

	private Crowd crowd;
	
	private JList list;
	
	private JTextArea textArea; //read
	
	private JTextArea textArea_1; //write
	
	private CrowdController controller = CrowdController.getInstance();
	/**
	 * Create the panel.
	 */
	public CrowdChartPanel() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 10, 391, 203);
		add(panel);
		panel.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 0, 391, 203);
		panel.add(scrollPane);
		
		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		textArea.setEditable(false);
		textArea.setLineWrap(true);
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(412, 10, 168, 427);
		add(panel_1);
		panel_1.setLayout(null);
		
		JPanel panel_5 = new JPanel();
		panel_5.setBounds(10, 10, 148, 115);
		panel_1.add(panel_5);
		
		JLabel lblImage = new JLabel("image");
		panel_5.add(lblImage);
		
		JPanel panel_6 = new JPanel();
		panel_6.setBounds(10, 129, 148, 288);
		panel_1.add(panel_6);
		panel_6.setLayout(null);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(10, 10, 128, 268);
		panel_6.add(scrollPane_2);
		
		list = new JList();
		scrollPane_2.setViewportView(list);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBounds(10, 223, 391, 214);
		add(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_3 = new JPanel();
		panel_2.add(panel_3, BorderLayout.SOUTH);
		
		JButton btnNewButton = new JButton("send");
		panel_3.add(btnNewButton);
		btnNewButton.addActionListener(this);
		
		JButton btnNewButton_1 = new JButton("close");
		panel_3.add(btnNewButton_1);
		btnNewButton_1.addActionListener(this);
		
		JPanel panel_4 = new JPanel();
		panel_2.add(panel_4, BorderLayout.CENTER);
		panel_4.setLayout(null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(0, 0, 391, 171);
		panel_4.add(scrollPane_1);
		
		textArea_1 = new JTextArea();
		textArea_1.setLineWrap(true);
		
		scrollPane_1.setViewportView(textArea_1);
		
		this.setSize(592, 440);
	}

	@SuppressWarnings("unchecked")
	public void setOwner(Crowd crowd){
		this.crowd = crowd;
		Set<User> users = crowd.getUsers();
		DefaultListModel<User> dlm = new DefaultListModel<User>();
		for(User u:users){
			dlm.addElement(u);
		}
		list.setModel(dlm);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("send")){
			String info = textArea_1.getText();
			try {
				controller.sendMessage(crowd,info);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			textArea_1.setText("");
			info = "【我】" + new SimpleDateFormat("HH-mm-ss").format(new Date())+ "\r\n" + info +"\r\n";
//			readAble.insert(info, readAble.getCaretPosition());
			textArea.append(info);
			textArea.setCaretPosition(textArea.getText().length());  
		}
		
	}

	public void messageArrived(MessageEvent event) {
		Message ms = event.getSource();
		Logger.getLogger(CrowdChartPanel.class).debug("获取消息  ："+event);
		if(ms.getMessageType() == MessageType.CROWD_CHART){
			String info = ((CrowdChartMessage) ms).getMes();
			info = "【" + ((CrowdChartMessage) ms).getAccount() +"】" + new SimpleDateFormat("HH-mm-ss").format(new Date())+ "\r\n" + info + "\r\n";
			textArea.append(info);
			textArea.setCaretPosition(textArea.getText().length());  
		}
		
	}

	public void insertMessage(CrowdChartMessage cm) {
		String info = cm.getMes();
		info = "【" + cm.getAccount() +"】" + new SimpleDateFormat("HH-mm-ss").format(new Date())+ "\r\n" + info +"\r\n"; 
		textArea.append(info);
		textArea.setCaretPosition(textArea.getText().length());    	
	}

}
